//
//  ModifyNameViewController.m
//  parent
//
//  Created by IreWesT on 15/5/29.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "ModifyNameViewController.h"

@interface ModifyNameViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UITextField *textField;

@end

@implementation ModifyNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.view.backgroundColor = UIColorMake(244, 244, 244);

    UIView *temp_view = [[UIView alloc]initWithFrame:CGRectMake(0, 8, APP_SCREEN_WIDTH, 40)];
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, APP_SCREEN_WIDTH - 10, 40)];
    _textField.text = [[UserDao getInstance] getUserInfo].nickName;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.delegate = self;
    [temp_view addSubview:_textField];
    temp_view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:temp_view];
    
//    CALayer *border = [CALayer layer];
//    border.backgroundColor = DefaultBorderColor.CGColor;
//    border.frame = CGRectMake(0, _textField.bottom, APP_SCREEN_WIDTH, .5f);
//    [self.view.layer addSublayer:border];
    
    [_textField becomeFirstResponder];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20 , 44, 44)];
    button.showsTouchWhenHighlighted = YES;
    button.backgroundColor = [UIColor clearColor];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
    [button setImage:[UIImage imageNamed:@"nav_btn_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
    self.navigationItem.backBarButtonItem=backButton;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveButton)];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (_textField == textField) {
        [textField resignFirstResponder];
    }
    return YES;
}

-(void)saveButton{
    NSString *text = [_textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (text.length == 0) {
        [self ShowExclaHud:@"姓名不能为空"];
        return;
    }
    
    NSLog(@"%@", _textField.text);
    [_textField resignFirstResponder];
    
    [self apiChangeUserName:text];
}


-(void)backButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Networking

-(BOOL)apiChangeUserName:(NSString *)name {
    
    [super showHud:@"修改中" isDim:YES];
    
    NSString* uid = [NSString stringWithFormat:@"%ld",(long)[[UserDao getInstance] getUserId]];
    NSString* utype = @"1";
    
    NSDictionary *temp_dict = @{@"uid":uid,
                                @"utype":utype,
                                @"token":[[UserDao getInstance]getAccessToken],
                                @"newname":name};
    
    NSString *dataString = [JsonUtil convertDictionaryToJson:temp_dict];
    
    NSString *msg = @"USER_MODIFY_NAME";
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:msg data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            //NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [super showHudComplete:@"修改失败" success:NO];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                if ([[dict objectForKey:@"retCode"] integerValue]== 1) {
                    
                    [super showHudComplete:@"修改成功" success:YES];
                    NSDictionary *dict = @{@"nickname":name};
                    [[UserDao getInstance] updateUserInfo:dict withReload:YES];
                }else{
                    [super showHudComplete:@"修改失败" success:NO];
                }
            }else{
                [super showHudComplete:@"服务器返回数据失败" success:NO];
            }
        }
    }];
    return YES;
}

@end
